<?php 
namespace XFCore\traits\admin;
use lib\Examine;
trait PackageTrait{
    public function Packagestatus(){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $id = input("post")['id'];
        if(empty($id))exitjson(-1,'非法操作！');
        $row = $this->db->packagerow($id,'id','`id`,`status`');
        if(empty($row))exitjson(-1,'数据不存在!');
        $params = [
            'status' =>$row['status'] == 1 ? 0 : 1,
        ];
        if($this->db->update('package',$params,['id'=>$id]))exitjson(200,'更新状态成功');
        exitjson(-1,'更新状态失败！');
    }
    public function add_Package($get,$post,$data){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        Examine::checkRequiredParams([$data['name'],$data['days'],$data['quota'],$data['price']]);
        $params = [];
        foreach ($data as $k => $v) {
            if ($v !== '') {
                $params[$k] = $v;
            }
        }
        if($this->db->insert('package',$params))exitjson(200,'添加成功！');
        exitjson(-1,'添加失败！');
    }
    public function edit_Package($get,$post,$data){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $id = $data['id'];
        unset($data['id']);
        if(empty($id))exitjson(-1,'非法操作！');
        if(empty($this->db->packagerow($id,'id','`id`')))exitjson(-1,'套餐数据不存在');
        Examine::checkRequiredParams([$data['name'],$data['days'],$data['quota'],$data['price']]);
        $params = [];
        foreach ($data as $k => $v) {
            if ($v !== '') {
                $params[$k] = $v;
            }
        }
        if($this->db->update('package',$params,['id'=>$id]))exitjson(200,'修改成功！');
        exitjson(-1,'修改失败！');
    }
    public function Packageremove(){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $id = input("post")['id'];
        if(empty($id))exitjson(-1,'非法操作！');
        if(empty($this->db->packagerow($id,'id','`id`')))exitjson(-1,'套餐数据不存在');
        
        if($this->db->delete("package",['id'=>$id]))exitjson(200,'删除成功！');
        exitjson(-1,'删除失败！');
        
    }
}